package so.shanku.essential.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import aym.util.getservicesdata.GetServicesDataQueue;
import aym.util.getservicesdata.GetServicesDataUtil;
import aym.util.json.JsonMap;
import aym.util.json.JsonParseHelper;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.adapter.GoodsCommentsAdapter;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.JsonKeys;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.ExtraKeys;
import striveen.util.used.view.view.XListView;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Jaelyn on 16/3/3.
 */
public class GoodsCommentsActivity extends BaseUIActivity {
    //商品评论
    @ViewInject(id = R.id.goods_comments_xlv)
    private XListView goods_comments_xlv;
    @ViewInject(id = R.id.nodata_layout) //无数据
    private LinearLayout nodataLayout;
    @ViewInject(id = R.id.empty)
    private TextView emptyTv;
    private GoodsCommentsAdapter goodsCommentsAdapter;
    /**
     * 平乱
     */
    private ArrayList<JsonMap<String, Object>> commentsDatas=new ArrayList<JsonMap<String, Object>>();
    private int commentsCurrentPage = 0;
    private int pageSize = 10;
    private String goodsId;//发现id

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goods_comments_layout);
        goods_comments_xlv.setXListViewListener(loadXListViewMoreListener);
        goods_comments_xlv.setPullRefreshEnable(true);
        goods_comments_xlv.setPullLoadEnable(true);
        goodsCommentsAdapter = new GoodsCommentsAdapter(this);
        goodsCommentsAdapter.setDatas(commentsDatas);
        goods_comments_xlv.setAdapter(goodsCommentsAdapter);
        goodsId = getIntent().getStringExtra(Constant.ID);
        goodsId="100";
        loadingToast.show();
        getCommentList(0);
    }

    /**
     * 加载评论
     */
    private XListView.IXListViewListener loadXListViewMoreListener = new XListView.IXListViewListener() {
        @Override
        public void onRefresh() {
            getCommentList(0);
        }

        @Override
        public void onLoadMore() {
            getCommentList(1);
        }
    };

    /**
     * 获取评论
     * @param i 0
     */
    public void getCommentList(int i) {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);

        params.put("DiscoverId", goodsId);
        // params.put("EventId", nowShowProgram);
        params.put("currentPage", commentsCurrentPage + 1);
        params.put("pageSize", pageSize);

        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_GetDiscoverCommentsList);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
         queue.setTag(i);
        queue.setWhat(GetDataConfing.What_GetDiscoverCommentsList);
        getDataUtil.getData(queue);

    }

    private void validateData() {
        if (commentsDatas == null || commentsDatas.isEmpty()) {
            goods_comments_xlv.setVisibility(View.GONE);
            nodataLayout.setVisibility(View.VISIBLE);
        } else {
            goods_comments_xlv.setVisibility(View.VISIBLE);
            nodataLayout.setVisibility(View.GONE);
        }
    }

    private GetServicesDataUtil.IGetServicesDataCallBack callBack = new GetServicesDataUtil.IGetServicesDataCallBack() {

        @Override
        public void onLoaded(GetServicesDataQueue entity) {
            if (entity.isOk()) {
                if (ShowGetDataError.isCodeSuccess(mContext, entity.getInfo())) {
                    if (entity.what == GetDataConfing.What_GetDiscoverCommentsList) {
                        ArrayList<JsonMap<String, Object>> commentsDatas_temp = (ArrayList<JsonMap<String, Object>>) JsonParseHelper.getJsonMap_List_JsonMap(entity.getInfo(), JsonKeys.Key_Info);
                        setCommentsListData(commentsDatas_temp,(Integer)entity.getTag());
                        validateData();
                    }
                }
            } else {
                ShowGetDataError.showNetError(mContext);
            }
            loadingToast.dismiss();
            goods_comments_xlv.stopLoadMore();
            goods_comments_xlv.stopRefresh();
        }

    };

    private void setCommentsListData(ArrayList<JsonMap<String, Object>> commentsDatas_temp, Integer tag) {
        if (commentsDatas_temp.size() == 0) {
            goods_comments_xlv.setPullLoadEnable(false);
        } else {
            goods_comments_xlv.setPullLoadEnable(true);
        }
        if (tag == 0) {
            this.commentsDatas.addAll(0,commentsDatas_temp);
        } else {
            this.commentsDatas.addAll(commentsDatas_temp);
        }
        goodsCommentsAdapter.notifyDataSetChanged();
    }

}